package com.jbp.job;

import com.jbp.domin.LuckReq2;
import com.jbp.model.User;
import com.jbp.model.Wallet;
import com.jbp.redis.RedisConfigService;
import com.jbp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

/**
 * 测试任务
 * */
//@Component
//@Configuration
public class TestJob {
    @Autowired
    private LotteryService lotteryService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private UserDailyLuckRecordService userDailyLuckRecordService;

//    @Scheduled(fixedDelay = 1000*1000*60)
    public void setData(){
        int[] prices = new int[]{10, 20,50,1000};
//        int[] amounts = new int[]{10000,100000,1000000,2000000,5000000};
        int[] amounts = new int[]{30000};
        for (int j = 0; j < prices.length; j++) {
            for (int m = 0; m < amounts.length; m++) {
                for (int i = 0; i < 10; i++) {
                    int finalJ = j;
                    int finalM = m;
                    new Thread(
                            ()->{
                                for (int k = 0; k <1000; k++) {
                                    User user = new User();
                                    user.setMobile(new Random().nextInt(900000)+""+ finalJ+ finalM );
                                    user.setName(new Random().nextInt(90000)+""+ finalJ +finalM);
                                    user.setIp(new Random().nextInt(100000)+"");
                                    user.setDevice(new Random().nextInt(100000)+"");
                                    loginService.createUser(user);

                                    Wallet wallet = walletService.getByUserId(user.getId());
                                    wallet.setXinzuan(BigDecimal.valueOf(amounts[finalM]));
                                    walletService.updateWithLock(wallet);

                                    int price = prices[finalJ];
                                    LuckReq2 req = new LuckReq2();
                                    req.setPrice(price);
                                    req.setUserId(user.getId());
                                    req.setTimes(100);
                                    req.setRoomId(new Random().nextInt(100000));

                                    while (wallet.getXinzuan().intValue()>=price*100) {
                                        lotteryService.luck2(req);
                                        wallet = walletService.getByUserId(user.getId());
                                        HashMap<String, BigDecimal> map = userDailyLuckRecordService.getLuckByUser(user.getId());
                                        int pay = map.get("pay").intValue();
                                        int income = map.get("income").intValue();
                                        if (income*0.85>pay) {
                                            break;
                                        }
                                    }
                                }
                            }
                    ).start();

                }
            }
        }

    }
}

